{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f727858b",
   "metadata": {},
   "source": [
    "# 00 Hello Kernel\n",
    "最基础 Triton kernel 示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0fd2aba8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Output: tensor([ 1.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11., 12., 13.,\n",
      "        14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., 26., 27.,\n",
      "        28., 29., 30., 31.], device='cuda:0')\n"
     ]
    }
   ],
   "source": [
    "import triton\n",
    "import triton.language as tl\n",
    "import torch\n",
    "\n",
    "@triton.jit\n",
    "def hello_kernel(X_ptr, BLOCK_SIZE: tl.constexpr):\n",
    "    pid = tl.program_id(0)\n",
    "    offset = pid * BLOCK_SIZE\n",
    "    x = tl.load(X_ptr + offset)\n",
    "    tl.store(X_ptr + offset, x + 1)\n",
    "\n",
    "x = torch.arange(32, device='cuda', dtype=torch.float32)\n",
    "hello_kernel[(1,)](x, BLOCK_SIZE=32)\n",
    "print(\"Output:\", x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20190a3b-ed3d-41c8-9f1c-18f1ac97ba34",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0rc1"
  },
  "title": "Hello Kernel"
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
